home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
DARECNO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-11
|
5KB
|
156 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: darecno.c
// Title: Data File Build Utility Library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains code to
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <da.h>
#include <dio.h>
//----------------------------------------------------------------------------
// This constant is the size of each block. Since this is a two level
// file (at most), the maximum number of blocks must be
// <= BLOCK_SIZE * BLOCK_SIZE
//
// Current size is 768 which allows for a 589 Mb data file with 1024 byte
// blocks
//----------------------------------------------------------------------------
#define BLOCK_SIZE (768)
//----------------------------------------------------------------------------
// Description: Write record count information to data file.
// Parameters:
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E DataRecNo(PDATACFG pcfg)
{
FLAG16 fs = FL_OPEN|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
HF hfRecNo, hf = -1;
BOOL fResult = FALSE;
FPOS fsize, fpos;
SIZET cRecNo;
LONG aulRecNo[BLOCK_SIZE]; // Record # index buffer
SIZET cRecNo2 = 0; // Level 2
LONG aulRecNo2[BLOCK_SIZE]; // Level 2 buffer
USHORT ausRecNo[BLOCK_SIZE]; // Input buffer
ULONG ulRecords = 0;
BOOL f2Level = FALSE;
if (pcfg->fVerbose)
Output("Building record number index.\n");
if (!FileOpen(&hfRecNo, pcfg->szRecNo, fs, NULL))
return FALSE;
if (pcfg->fVerbose)
Output("Opened file '%s'\n.\n", pcfg->szRecNo);
fsize = FileGetSize(hfRecNo);
if (fsize < 0)
goto ERROR_EXIT;
Assert(fsize > 0 && (fsize % sizeof(USHORT)) == 0);
fsize /= sizeof(USHORT);
Assert(fsize <= (FPOS)BLOCK_SIZE * (FPOS)BLOCK_SIZE);
if (!FileSetPos(hfRecNo, 0, FL_BEGIN))
goto ERROR_EXIT;
if (!DioAppend(pcfg->szData, pcfg->szLogical, DFT_ISAM_RECNO,
sizeof(aulRecNo), &hf, NULL))
goto ERROR_EXIT;
fpos = FileGetPos(hf);
memset(aulRecNo2, 0, sizeof(aulRecNo2));
if (fsize > BLOCK_SIZE)
{
if (!FileWrite(hf, aulRecNo2, sizeof(aulRecNo2), fpos))
goto ERROR_EXIT;
f2Level = TRUE;
}
while (fsize)
{
SIZET cRead = (SIZET)MIN(fsize, (FPOS)BLOCK_SIZE);
if (!FileRead(hfRecNo, ausRecNo, cRead * sizeof(USHORT), -1))
goto ERROR_EXIT;
for (cRecNo = 0; cRecNo < cRead; ++cRecNo)
{
ulRecords += (ULONG)ausRecNo[cRecNo];
aulRecNo[cRecNo] = ulRecords;
}
for (; cRecNo < BLOCK_SIZE; ++cRecNo)
aulRecNo[cRecNo] = ulRecords;
aulRecNo2[cRecNo2++] = ulRecords;
if (!FileWrite(hf, aulRecNo, sizeof(aulRecNo), -1))
goto ERROR_EXIT;
fsize -= (FPOS)cRead;
}
if (f2Level)
{
for (; cRecNo2 < BLOCK_SIZE; ++cRecNo2)
aulRecNo2[cRecNo2] = ulRecords;
if (!FileWrite(hf, aulRecNo2, sizeof(aulRecNo2), fpos))
goto ERROR_EXIT;
}
fResult = TRUE;
ERROR_EXIT:
if (hf >= 0) // Update data file
if (!DioAppendClose(fResult, TRUE))
fResult = FALSE;
if (!FileClose(hfRecNo))
fResult = FALSE;
if (fResult && !FnameDelete(pcfg->szRecNo))
fResult = FALSE;
if (pcfg->fVerbose)
Output("Done.\n.\n");
return fResult;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------